extends Control
signal camera_change

# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var warp
var list
var trackers = []
var canvas
var tex
var img

# Called when the node enters the scene tree for the first time.
func _ready():
	canvas = get_node("CanvasLayer/trackers")
	warp = get_node("CanvasLayer/HSlider/Label")
	list = get_node("CanvasLayer/ItemList")
	warp.text = "Timewarp! (" + str(Global.timewarp) + "x)"
	tex = ImageTexture.new()
	img = load("res://assets/tracker.png")
	tex.create_from_image(img)
	update_orbitals()
	pass # Replace with function body.

func update_orbitals():
		trackers = []
		list.clear()
		for child in canvas.get_children():
			canvas.remove_child(child)
			child.queue_free()
		for child in Global.objects:
			list.add_item(child.name)
			if(child.name != "Sun"): #Don't add a tracker for the sun
				var tracker = Sprite.new()
				trackers += [tracker]
				tracker.texture = tex
				tracker.modulate = child.get_parent().color
				tracker.name = child.name
				tracker.scale = Vector2(0.5,0.5)
				canvas.add_child(tracker)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	for tracker in trackers:
		for obj in Global.objects:
			if obj.name == tracker.name:
				if obj.get_node("vis").is_on_screen() == true:
					tracker.show()
					tracker.position = Global.camera.unproject_position(obj.get_global_transform().origin)
				else:
					tracker.hide()
		pass
	pass


func _on_HSlider_value_changed(value):
	Global.timewarp = pow(10,value)
	warp.text = "Timewarp! (" + str(Global.timewarp) + "x)"
	pass # Replace with function body.


func _on_ItemList_item_selected(index):
	for node in Global.objects:
		if node.name == list.get_item_text(index):
			emit_signal("camera_change", node)
	pass # Replace with function body.


func _on_Sun_update_orbits():
	update_orbitals()
	get_node("CanvasLayer/Button").hide()
	pass # Replace with function body.
